/*
 * Copyright 2007 Jaisy Sprl.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package be.jaisy.ui.gwt.widget.client.viewer;

/**
 * A content viewer is a model-based adapter on a Composite which accesses its
 * model by means of a content provider and a label provider.
 * 
 * @author Stany Blanvalet <stany.blanvalet@jaisy.be>
 */
public abstract class ContentViewer extends Viewer {

    /**
     * This viewer's input, or <code>null</code> if none.
     * The viewer's input provides the "model" for the viewer's content.
     */
    private Object input = null;
	
    /**
     * This viewer's content provider, or <code>null</code> if none.
     */
    private IContentProvider contentProvider = null;

    /**
     * This viewer's label provider, or <code>null</code> if none.
     */
    private IBaseLabelProvider labelProvider = null;

    /**
     * Creates a content viewer with no input, no content provider, and no label provider.
     */
    protected ContentViewer() {
   	 //
    }
    
    /**
     * The <code>ContentViewer</code> implementation of this <code>IInputProvider</code> 
     * method returns the current input of this viewer, or <code>null</code>
     * if none. The viewer's input provides the "model" for the viewer's
     * content.
     */
    public Object getInput() {
        return input;
    }

    /**
     * Returns the content provider used by this viewer, 
     * or <code>null</code> if this view does not yet have a content
     * provider.
     * 
     * @return the content provider, or <code>null</code> if none
     */
    public IContentProvider getContentProvider() {
        return contentProvider;
    }

    /**
     * Returns the label provider used by this viewer or <code>null</code> if this view does not yet have a label
     * provider.     
     *
     * @return a label provider
     */
    public IBaseLabelProvider getLabelProvider() {
        return labelProvider;
    }

    /**
     * The <code>ContentViewer</code> implementation of this <code>Viewer</code>
     * method invokes <code>inputChanged</code> on the content provider and then the
     * <code>inputChanged</code> hook method.
     */
    public void setInput(Object input) {
        Object oldInput = getInput();
        if(contentProvider!=null)
      	  contentProvider.inputChanged(this, oldInput, input);
        this.input = input;
        inputChanged(this.input, oldInput);
    }
    
    /**
     * Set the content provider used by this viewer.
     */
    public void setContentProvider(IContentProvider contentProvider) {
    	this.contentProvider = contentProvider;
    }

    /**
     * Set the label provider for this viewer.
     */
    public void setLabelProvider(IBaseLabelProvider labelProvider) {
    	this.labelProvider = labelProvider; 
    }
}